import { getSocialUserPage, SocialUserVO } from '@/services/SocialUserService'
import { getSocialUserTagList, removeSocialUserTag, updateSocialUserTag } from '@/services/SocialUserTagService'
import { makeAutoObservable, runInAction, when } from 'mobx'
import { SocialUserTag, SocialUserTagUpdateBO, SocialUserTagUpdateDTO } from 'types/tag'

class SocialUserTagModel {
  socialUserTagList: { first: string; tags: SocialUserTag[] }[] = []
  currentTag: SocialUserTag = {
    createTime: '',
    id: '0',
    socialUserTotal: 0,
    tagBgColor: '',
    tagName: '',
    tagTextColor: '',
  }
  tagDrawerState = false
  currentTagSocialUserList: SocialUserVO[] = []
  socialUserPages = 0
  socialUserTotal = 0
  socialUserCurrentPage = 0
  socialUserCurrentPageSize = 20

  async loadMoreCurrentTagSocialUser(current: number = this.socialUserCurrentPage) {
    let data = await getSocialUserPage({
      tagId: this.currentTag.id,
      size: this.socialUserCurrentPageSize,
      current: current,
    })
    runInAction(() => {
      this.currentTagSocialUserList.push(...data.data)
      this.socialUserTotal = data.total
      this.socialUserPages = data.pages
    })
  }

  closeDrawer() {
    this.tagDrawerState = false
  }

  constructor() {
    makeAutoObservable(this, {}, { autoBind: true })
  }

  nextPage() {
    this.socialUserCurrentPage++
  }

  setCurrentTag(tag: SocialUserTag) {
    this.currentTag = tag
    this.currentTagSocialUserList = []
    this.socialUserCurrentPage = 1
    this.loadMoreCurrentTagSocialUser().then()
    this.tagDrawerState = true
  }

  async getSocialUserTagListAsync() {
    const socialUserTagList = await getSocialUserTagList()
    let socialUserTagGroup = socialUserTagList.reduce((acc, curr) => {
      let f = curr.tagName.slice(0, 1)
      let g = acc.find((i) => i.first === f)
      if (g) {
        g.tags.push(curr)
      } else {
        acc.push({ first: f, tags: [curr] })
      }
      return acc
    }, [] as { first: string; tags: SocialUserTag[] }[])
    runInAction(() => (this.socialUserTagList = socialUserTagGroup))
  }

  // 编辑
  updateModalState = false
  updateTagBO: SocialUserTagUpdateBO = { tagName: '' }

  /**
   * 设置标签编辑模态框状态
   * @param state 状态
   */
  setUpdateModalState(state: boolean) {
    this.updateModalState = state
  }

  get isUpdate() {
    return this.updateTagBO.id !== undefined
  }

  /**
   * 创建标签
   */
  createTag() {
    this.updateTagBO = {
      tagName: '默认标签名称',
      tagTextColor: '#000000',
      tagBgColor: '#EEEEEE',
    }
    this.setUpdateModalState(true)
  }

  /**
   * 打开标签编辑模态框
   */
  updateTag(vo: SocialUserTag) {
    this.updateTagBO = { ...vo }
    this.setUpdateModalState(true)
  }

  /**
   * 保存标签j
   * @param bo 业务类
   */
  async saveTag(bo: SocialUserTagUpdateBO) {
    let dto: SocialUserTagUpdateDTO = { ...bo }
    let res = await updateSocialUserTag(dto)

    if (dto.id === undefined) {
      // 新建标签的副作用
      this.getSocialUserTagListAsync().then()
    } else {
      // 更新标签的副作用
      this.currentTag = res
      this.getSocialUserTagListAsync().then()
    }
    return res !== undefined
  }

  /**
   * 删除标签
   * @param id 标签ID
   */
  async deleteTag(id: string) {
    return await removeSocialUserTag(id)
  }
}

export default SocialUserTagModel
export const socialUserTagModel = new SocialUserTagModel()

when(
  () => socialUserTagModel.socialUserCurrentPage > 1,
  () => {
    socialUserTagModel.loadMoreCurrentTagSocialUser().then()
  },
)
